package com.smart.community.region.config;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * 住户管理模块缓存配置
 * 
 * @author Wu.Liang
 * @since 2025-07-28
 */
@Configuration
public class ResidentCacheConfig {

    /**
     * 住户管理缓存管理器
     * 
     * @param connectionFactory Redis连接工厂
     * @return Redis缓存管理器
     */
    @Bean("residentCacheManager")
    public CacheManager residentCacheManager(RedisConnectionFactory connectionFactory) {
        // 默认缓存配置
        RedisCacheConfiguration defaultConfig = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofMinutes(30)) // 默认30分钟过期
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()))
                .disableCachingNullValues(); // 不缓存null值

        // 不同缓存区域的配置
        Map<String, RedisCacheConfiguration> cacheConfigurations = new HashMap<>();
        
        // 住户基本信息缓存 - 变化频率低，缓存时间较长
        cacheConfigurations.put("resident", defaultConfig.entryTtl(Duration.ofHours(2)));
        
        // 住户房户关联缓存 - 变化频率中等，缓存时间适中
        cacheConfigurations.put("residentHousehold", defaultConfig.entryTtl(Duration.ofMinutes(30)));
        
        // 房户信息缓存 - 变化频率低，缓存时间较长
        cacheConfigurations.put("household", defaultConfig.entryTtl(Duration.ofHours(1)));
        
        // 住户选项缓存 - 变化频率低，缓存时间较长
        cacheConfigurations.put("residentOptions", defaultConfig.entryTtl(Duration.ofHours(1)));
        
        // 住户统计信息缓存 - 变化频率中等，缓存时间适中
        cacheConfigurations.put("residentStatistics", defaultConfig.entryTtl(Duration.ofMinutes(15)));
        
        // 住户搜索缓存 - 变化频率高，缓存时间较短
        cacheConfigurations.put("residentSearch", defaultConfig.entryTtl(Duration.ofMinutes(5)));

        return RedisCacheManager.builder(connectionFactory)
                .cacheDefaults(defaultConfig)
                .withInitialCacheConfigurations(cacheConfigurations)
                .build();
    }

    /**
     * 本地缓存管理器（备用方案）
     * 
     * @return 本地缓存管理器
     */
    @Bean("residentLocalCacheManager")
    public CacheManager residentLocalCacheManager() {
        return new ConcurrentMapCacheManager(
                "resident",
                "residentHousehold", 
                "household",
                "residentOptions",
                "residentStatistics",
                "residentSearch"
        );
    }
} 